package com.shangbang.verb.service.impl.record;

import com.shangbang.verb.dao.record.LikeMapper;
import com.shangbang.verb.dao.record.StoryMapper;
import com.shangbang.verb.dao.record.UserRecordMapper;
import com.shangbang.verb.entity.record.LikeEntity;
import com.shangbang.verb.entity.record.StoryEntity;
import com.shangbang.verb.entity.record.UserRecordEntity;
import com.shangbang.verb.service.record.StoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StoryServiceImpl implements StoryService {
    @Autowired
    private StoryMapper storyMapper;
    @Autowired
    private UserRecordMapper userMapper;
    @Autowired
    private LikeMapper likeMapper;
    @Override
    public void createStory(String title, String duration, String music_url, String images, Integer uid) {

        UserRecordEntity userEntity = userMapper.selectById(uid);
        if(userEntity!=null){
            StoryEntity storyEntity = new StoryEntity();
            storyEntity.setTitle(title);
            storyEntity.setDuration(duration);
            storyEntity.setMusicUrl(music_url);
            storyEntity.setImages(images);
            storyEntity.setUid(uid);
            storyEntity.setTime(new Date());
            storyEntity.setIcon(userEntity.getIcon());
            String[] sourceStrArray = images.split(",");
            storyEntity.setCover(sourceStrArray[0]);
            storyMapper.insert(storyEntity);
        }
    }

    @Override
    public Object getStoryInfo(Integer story_id) {

        StoryEntity storyEntity = storyMapper.selectById(story_id);

        return storyEntity;
    }

    @Override
    public Object getStoryList(Integer uid, Integer page, Integer num, Integer type) {
        List<StoryEntity> storyEntityList = storyMapper.getStoryList(uid,page,num,type);
        if(storyEntityList!=null&&storyEntityList.size()>0){
            for (int i = 0; i <storyEntityList.size() ; i++) {
                LikeEntity likeEntity = likeMapper.getLikeInfo(uid,storyEntityList.get(i).getId());
                if(null!=likeEntity){
                    storyEntityList.get(i).setState(1);
                }
            }
        }
        return storyEntityList;
    }
}